# encoding: utf-8
# @File  : test03_add_cart.py
# @Author: wu shaofan
# @Date  :  2024/04/17
# @Desc : 用例描述：登录账号进入购物车，搜索商品添加到购物车，点击购物车按钮，根据商品名称点击删除按钮，点击弹框中删除按钮
import json
import allure
import pytest
import config
import conftest
from PO.page.page_cart import PageCart
from PO.page.page_index import PageIndex
from PO.page.page_login import PageLogin
from PO.page.page_user_info import PageUserinfo
from Utils.get_driver import GetDriver
from config import login_url, testdata
from Utils.record_log import Log


@allure.feature('2、购物车模块')
class TestAddCar:
    # 初始化
    def setup_class(self):
        # 获取driver
        self.driver = GetDriver().get_driver(login_url)
        # 异常截图
        conftest.driver = self.driver
        # 获取登录页面对象
        self.login = PageLogin(self.driver)
        # 获取用户个人中心对象
        self.userinfo = PageUserinfo(self.driver)
        # 获取首页对象
        self.index = PageIndex(self.driver)
        # 获取购物车对象
        self.cart = PageCart(self.driver)
        # 获取日志对象
        self.log = Log("商品加入购物车用例").get_logger()

    def teardown_class(self):
        # 关闭driver
        GetDriver().close_driver()
        Log("搜索商品添加到购物车用例").write_empty_line()

    # 测试用例
    @allure.story("1、商品加入购物车")
    def test_addcart(self):
        with allure.step("前置条件：登录测试账号"):
            # 输入用户名, 密码, 验证码, 点击登录按钮
            self.login.page_input_username(testdata["用户名"])
            self.login.page_input_password(testdata["密码"])
            self.login.page_input_verify_code(testdata["验证码"])
            self.login.page_click_login_btn()
            # allure截图
            self.login.base_get_allure_image()
        # allure报告记录用户信息
        data = json.dumps(testdata, ensure_ascii=False, indent=4)
        allure.attach(data, "登录用户信息", allure.attachment_type.JSON)

        with allure.step("测试步骤一：点击返回商城首页链接"):
            # self.userinfo.base_refresh()
            self.userinfo.page_click_back_home_btn()

        with allure.step("测试步骤二：搜索框输入“小米8"):
            self.index.page_input_search_info(config.search_product_name)
            self.login.base_get_allure_image()

        with allure.step("测试步骤三：点击搜索按钮"):
            self.index.page_click_search_btn()
            self.login.base_get_allure_image()

        with allure.step("测试步骤四：滚动滑动条向下滑"):
            self.index.base_move_scroll_bar(200)
            self.login.base_get_allure_image()

        with allure.step("测试步骤五：添加数量"):
            self.index.page_add_goods_num(5)
            self.login.base_get_allure_image()

        with allure.step("测试步骤六：点击加入购物车按钮"):
            self.index.page_click_add_cart_btn()
            self.login.base_get_allure_image()

        with allure.step("测试步骤七：进入iframe框架"):
            self.index.page_into_iframe01()

        with allure.step("测试步骤八：获取加入购物车成功提示信息"):
            result = self.index.page_get_add_cart_success_info()
            self.log.info(f"商品是否添加成功:{result}")
            self.login.base_get_allure_image()
            # TODO 退出iframe框架
            self.driver.switch_to.default_content()

        with allure.step("测试步骤九：点击关闭弹框按钮"):
            self.index.page_click_close_btn()
            self.login.base_get_allure_image()

        with allure.step("测试步骤十：滚动滑动条向下滑"):
            self.index.base_move_scroll_bar(-200)
            self.login.base_get_allure_image()

        with allure.step("测试步骤十一：点击购物车按钮"):
            self.index.page_click_cart_btn()
            self.login.base_get_allure_image()

        with allure.step("测试步骤十二：根据商品名称点击删除按钮"):
            goods = self.cart.page_get_cart_goods_name_del(config.search_product_name)
            self.log.info(f"购物车中商品列表:{goods}")
            self.login.base_get_allure_image()

        with allure.step("测试步骤十三：点击弹框中删除按钮"):
            self.cart.page_click_del_button()
            self.cart.base_refresh()
            self.login.base_get_allure_image()


if __name__ == '__main__':
    pytest.main()
